package tcp;


import com.gettyio.core.channel.SocketChannel;
import com.gettyio.core.channel.config.ServerConfig;
import com.gettyio.core.channel.starter.AioServerStarter;
import com.gettyio.core.handler.codec.websocket.WebSocketDecoder;
import com.gettyio.core.handler.codec.websocket.WebSocketEncoder;
import com.gettyio.core.handler.ssl.ClientAuth;
import com.gettyio.core.handler.ssl.SslConfig;
import com.gettyio.core.handler.ssl.SslService;
import com.gettyio.core.pipeline.ChannelInitializer;
import com.gettyio.core.pipeline.DefaultChannelPipeline;

import java.net.StandardSocketOptions;

public class WsServer {


    public static void main(String[] args) {
        //System.setProperty("javax.net.debug", "all");
        try {
            //初始化配置对象
            ServerConfig aioServerConfig = new ServerConfig();
            //设置host,不设置默认localhost
            //aioServerConfig.setHost("127.0.0.1");
            //设置端口号
            aioServerConfig.setPort(8888);
            //设置服务器端内存池最大可分配空间大小，默认256mb，内存池空间可以根据吞吐量设置。
            // 尽量可以设置大一点，因为这不会真正的占用系统内存，只有真正使用时才会分配
            aioServerConfig.setServerChunkSize(512 * 1024 * 1024);
            //设置数据输出器队列大小，一般不用设置这个参数，默认是10*1024*1024
            aioServerConfig.setBufferWriterQueueSize(10 * 1024 * 1024);
            //设置读取缓存块大小，一般不用设置这个参数，默认128字节
            aioServerConfig.setReadBufferSize(2048);
            //设置内存池等待分配内存的最大阻塞时间，默认是1秒
            aioServerConfig.setChunkPoolBlockTime(1000);
            //设置SocketOptions
            aioServerConfig.setOption(StandardSocketOptions.SO_RCVBUF, 8192);

            AioServerStarter server = new AioServerStarter(aioServerConfig);
            server.channelInitializer(new ChannelInitializer() {
                @Override
                public void initChannel(SocketChannel channel) throws Exception {
                    //获取责任链对象
                    DefaultChannelPipeline defaultChannelPipeline = channel.getDefaultChannelPipeline();

                    //获取证书
                    String pkPath = getClass().getClassLoader().getResource("serverStore.jks")
                            .getPath();
                    //ssl配置
                    SslConfig sSLConfig = new SslConfig();
                    sSLConfig.setKeyFile(pkPath);
                    sSLConfig.setKeyPassword("123456");
                    sSLConfig.setKeystorePassword("123456");
                    sSLConfig.setTrustFile(pkPath);
                    sSLConfig.setTrustPassword("123456");
                    //设置服务器模式
                    sSLConfig.setClientMode(false);
                    //设置单向验证或双向验证
                    sSLConfig.setClientAuth(ClientAuth.NONE);
                    //初始化ssl服务
                    SslService sslService = new SslService(sSLConfig, "TLSv1.2");
                    //defaultChannelPipeline.addFirst(new SslHandler(channel, sslService));

                    defaultChannelPipeline.addLast(new WebSocketEncoder());
                    defaultChannelPipeline.addLast(new WebSocketDecoder());

                    defaultChannelPipeline.addLast(new SimpleHandler());

                    // ----配置Protobuf处理器----
//                    defaultChannelPipeline.addLast(new ProtobufDecoder(MessageClass.Message.getDefaultInstance()));
//                    defaultChannelPipeline.addLast(new ProtobufEncoder());
//                    defaultChannelPipeline.addLast(new PbSimpleHandler());
                    // ----Protobuf处理器END----

                }
            }).start();
            System.out.println("启动ws服务");
        } catch (Exception e) {

        }
    }

}
